﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace GreenPark
{
    static class CommonFun
    {
        public static bool isCmdExecSuccess(string resultStr)
        {
            int index = resultStr.IndexOf("success");
            if (index > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public static string getHWDeviceTime(string resultStr)
        {
            int start = resultStr.IndexOf("time=\"");
            string tempStr = resultStr.Substring(start + 6);
            int end = tempStr.IndexOf("\"");
            return tempStr.Substring(0, end);
        }

        public static string getDayLightStartTime(string resultStr)
        {
            int start = resultStr.IndexOf("start_time=");
            int end = resultStr.IndexOf("end_time=");
            string temp = resultStr.Substring(start, end - start);
            return temp;
        }

        public static string getDayLightEndTime(string resultStr)
        {
            int start = resultStr.IndexOf("end_time=");
            int end = resultStr.IndexOf(" )");
            string temp = resultStr.Substring(start, end - start);
            return temp;
        }

        public static string getWorkStatus(string resultStr)
        {
            int start = resultStr.IndexOf("success") + 9;
            int end = resultStr.IndexOf(")");
            string temp = resultStr.Substring(start, end - start);
            return temp;
        }

        public static List<string> getAllEmployeeID(string resultStr)
        {
            string pattern = "id=\"([0-9]+)\"";
            MatchCollection matchs = Regex.Matches(resultStr, pattern);
            List<string> employeeList = new List<string>();
            if (matchs != null)
            {
                foreach (Match match in matchs)
                {
                    int len = match.Value.Length;
                    string temp = match.Value.Substring(4, len - 5);
                    employeeList.Add(temp);
                }
            }
            return employeeList;
        }

        public static List<string> getAllPicName(string strResult)
        {
            List<string> picNameList = new List<string>();
            const string strSubStr = "name=\"";
            while (strResult.IndexOf(strSubStr) != -1)
            {
                int unStart = strResult.IndexOf(strSubStr) + strSubStr.Length;
                strResult = strResult.Substring(unStart);
                int unEnd = strResult.IndexOf("\"");
                string strPicName = strResult.Substring(0, unEnd);
                picNameList.Add(strPicName);
                strResult = strResult.Substring(unEnd);
            }
            return picNameList;
        }

        public static string getPicNameFromPath(string strPicPathName)
        {
            int nStart = strPicPathName.LastIndexOf('\\') + 1;
            string strPicName = strPicPathName.Substring(nStart, strPicPathName.Length - nStart);
            return strPicName;
        }

        public static bool isValidIPPort(string strIPPort)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(strIPPort, "[0-9]{1,6}"))
            {
                if (System.Int32.Parse(strIPPort) > 1024 && System.Int32.Parse(strIPPort) < 65535)
                {
                    return true;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public static bool isValidPassword(string strPwd)
        {
            if ((strPwd == "") || (System.Text.RegularExpressions.Regex.IsMatch(strPwd, "[0-9]{1,8}")))
            {
                return true;
            }
            else
                return false;
        }

        public static bool isValidIP(string ip)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(ip, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
            {
                string[] ips = ip.Split('.');
                if (ips.Length == 4)
                {
                    if ((System.Int32.Parse(ips[0]) >= 0 && System.Int32.Parse(ips[0]) <= 255)
                        && (System.Int32.Parse(ips[1]) >= 0 && System.Int32.Parse(ips[1]) <= 255)
                        && (System.Int32.Parse(ips[2]) >= 0 && System.Int32.Parse(ips[2]) <= 255)
                        && (System.Int32.Parse(ips[3]) >= 0 && System.Int32.Parse(ips[3]) <= 255))
                        return true;
                    else
                        return false;
                }
                else
                    return false;
            }
            else
                return false;
        }

        public static string formatIPAddress(string ip)
        {
            string strFormatIP = "";
            string[] ips = ip.Split('.');
            for (int i = 0; i < 4; i++)
            {
                if (ips[i].Length != 3)
                {
                    if (ips[i].Length == 1)
                    {
                        ips[i] = "00" + ips[i];
                    }
                    else
                    {
                        ips[i] = "0" + ips[i];
                    }
                }
                else
                {
                    ips[i] = ips[i];
                }
            }
            strFormatIP = ips[0] + "." + ips[1] + "." + ips[2] + "." + ips[3];

            return strFormatIP;
        }

        public static string removeIPDot(string ip)
        {
            string strIPnoDot = "";
            string[] ips = ip.Split('.');
            for (int i = 0; i < ips.Length; i++)
            {
                strIPnoDot += ips[i];
            }
            return strIPnoDot;
        }

        public static string removeTurkish(string deger)
        {
            return deger.Trim().Replace('Ö', 'O').Replace('Ş', 'S').Replace('İ', 'I').Replace('Ü', 'U').Replace('Ç', 'C').Replace('Ğ', 'G').Replace('ö', 'O').Replace('ş', 's').Replace('ı', 'i').Replace('ü', 'u').Replace('ç', 'c').Replace('ğ', 'g');
        }
    }
}
